package com.wangjiyuan.giftshuo.ui.activity.classify.strategy_details.group_details;

import android.content.Context;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.TextView;

import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.bean.classifybean.strategy.GroupDetailsBean;
import com.wangjiyuan.giftshuo.config.classify.ClassifyConstant;
import com.wangjiyuan.giftshuo.ui.activity.classify.strategy_details.group_details.adapter.GroupDetRecyclerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 品类、风格、对象点击item进入的详情画面
 */

public class GroupDetActivity extends AppCompatActivity implements GroupDetContract.IContractView{
    private Context mContext=this;
    private String titleName;
    private int id,limit=20,offset=0;
    private String order_by="now";

    private Toolbar mToolBar;
    private TextView mTextViewTitle;
    private RecyclerView mRecyclerView;
    private List<GroupDetailsBean.DataBean.ItemsBean> itemsBeanList=new ArrayList<>();
    private GroupDetRecyclerAdapter groupDetRecyclerAdapter;

    //    粘性的eventBus，获取ID、title
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onEvent(Map<String,String> map){
        titleName = map.get(ClassifyConstant.Strategy_MAP_NAME);
        String map_ID = map.get(ClassifyConstant.Strategy_MAP_ID);
        id = Integer.valueOf(map_ID).intValue();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_det);
//        注册eventBus
        EventBus.getDefault().register(this);

        initData();
        initView();
    }


    private void initView() {
        mToolBar= (Toolbar) findViewById(R.id.toolBar_group_det);
        mToolBar.setTitle("");
        initToolBar();
        mTextViewTitle= (TextView) findViewById(R.id.textView_group_det_title);
        mTextViewTitle.setText(titleName);

        mRecyclerView= (RecyclerView) findViewById(R.id.recyclerView_group_det);
        mRecyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(mContext, OrientationHelper.VERTICAL,false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        groupDetRecyclerAdapter=new GroupDetRecyclerAdapter(mContext,itemsBeanList);
        mRecyclerView.setAdapter(groupDetRecyclerAdapter);
    }

    private void initToolBar() {
        setSupportActionBar(mToolBar);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initData() {
        GroupDetPresenter groupDetPresenter=new GroupDetPresenter(this);
        groupDetPresenter.getGroupDetailObservable(id,limit,offset,order_by);
    }


    @Override
    public void getGroupDetailsData(GroupDetailsBean groupDetailsBean) {
        List<GroupDetailsBean.DataBean.ItemsBean> items = groupDetailsBean.getData().getItems();
        groupDetRecyclerAdapter.reloadRecyclerView(items,true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        解绑eventBus
        EventBus.getDefault().unregister(this);
    }
}
